package com.study.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.book.common.R;
import com.study.book.mapper.AdminMapper;
import com.study.book.pojo.Admin;
import com.study.book.pojo.Comment;
import com.study.book.service.CommentService;
import com.study.book.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yuzhenan
 * @description 针对表【Comment(评论帖子表)】的数据库操作Service实现
 * @createDate 2024-05-13 20:50:05
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
        implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public R Addcomment(Comment comment) {
        Admin admin = adminMapper.selectById(comment.getAdminid());
        if (admin == null) {
            return R.error("用户不存在");
        }
        comment.setAdminName(admin.getUsername());
        List<Comment> comments = commentMapper.selectList(new LambdaQueryWrapper<Comment>().eq(Comment::getForumid, comment.getForumid()));
        comment.setSort(comments.size());

        int insert = commentMapper.insert(comment);

        if (insert > 0) {
            return R.ok("评论成功");
        }
        return R.error("评论失败");
    }
}




